package com.quentinbeauperin.twitbird
{
	import com.coreyoneil.collision.CDK;
	import com.quentinbeauperin.twitbird.Bird;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	
	import uk.co.bigroom.input.KeyPoll;
	
	public class Engine
	{
		private var keyPoll:KeyPoll;
		
		public var stage:Stage;
		public var bird:Bird;
		public var rotatingMap:RotatingMap;
		public var ringGenerator1:RingGenerator;
		public var ringGenerator2:RingGenerator;
		public var obstacleGenerator1:ObstacleGenerator;
		public var obstacleGenerator2:ObstacleGenerator;
		public var birdX:int = new int();
		public var birdY:int = new int();
		public var randomY:int = new int();
		public var randomFrequencyRing:int = new int();
		public var randomFrequencyObstacle:int = new int();
		public var buttonPause:Bouton;
		public var screenEngine:ScreenEngine;
		public var game:Game;
		public var countLives:int = new int();
		public var level:int = new int();
		
		// Start the engine
		public function start():void
		{
			keyPoll = new KeyPoll(stage);
			stage.addEventListener(Event.ENTER_FRAME, loop);
		}
		
		// Loop where the listener are going to look at every frame
		private function loop(event:Event):void
		{
			ringEngine();
			obstacleEngine();
			rotatingMap.moveMap();
			birdX = bird.x;
			birdY = bird.y;
			move();
			
			buttonPause.addEventListener(MouseEvent.MOUSE_UP, windowMenuPause);
		}

		// Bring back the player to the menu
		private function windowMenuPause(event:Event):void
		{
			screenEngine = new ScreenEngine();
			stage.addChild(screenEngine);
		}
		
		// Instructions to create rings
		private function ringEngine():void
		{
		// Generate random numbers for the frequency of appearance of the rings
			randomY = Math.floor(Math.random()*381);
			randomFrequencyRing = Math.floor((Math.random()*30)+50);
			ringGenerator1.randomY = randomY;
			ringGenerator2.randomY = randomY;
			ringGenerator1.randomFrequency = randomFrequencyRing;
			ringGenerator2.randomFrequency = randomFrequencyRing;
			
		// Give instructions to ringGenerator 1 and 2
			ringGenerator1.createRing();
			ringGenerator2.createRing();
			ringGenerator1.count += 1;
			ringGenerator2.count += 1;
			
		// Make rings go forward
			ringGenerator1.x -= 8;
			ringGenerator2.x -= 8;
		}
		
		// Inctructions to create obstacles
		private function obstacleEngine():void
		{
		// Give instructions to obstacleGenerator
			obstacleGenerator1.createObstacle();
			obstacleGenerator1.count += 1;
			obstacleGenerator2.curentCount = obstacleGenerator1.curentCount;
			obstacleGenerator2.obstacleArray = obstacleGenerator1.obstacleArray;
			obstacleGenerator2.createObstacle();
			obstacleGenerator2.count += 1;
			obstacleGenerator1.curentCount = obstacleGenerator2.curentCount;
			obstacleGenerator1.obstacleArray = obstacleGenerator2.obstacleArray;
			
		// Make rings go forward
			if(level == 1){
				obstacleGenerator1.x -= 8;
				obstacleGenerator1.y += 4;
				obstacleGenerator2.x -= 12;
				obstacleGenerator2.y += 6
			}else if(level == 5){
				obstacleGenerator1.x -= 8;
				obstacleGenerator2.x -= 8;
			}
		}
		
		// Instructions to move the bird
		private function move():void
		{
			if (keyPoll.isDown(Keyboard.RIGHT)){
				// block the bird into the window				
				if (bird.x > 750){
					bird.x = bird.x;
				}
				// move the bird
				else{
					bird.x += 10;
				}
			}
			if (keyPoll.isDown(Keyboard.LEFT)){
				if (bird.x < 10){
					bird.x = bird.x;
				}
				else{
					bird.x -= 5;
				}
			}
			if (keyPoll.isDown(Keyboard.DOWN)){
				if (bird.y > 400){
					bird.y = bird.y;
				}
				else{
					bird.y += 8;
				}
			}
			if (keyPoll.isDown(Keyboard.UP)){
				if (bird.y < 10){
					bird.y = bird.y;
				}
				else{
					bird.y -= 8;
				}
			}
		}
		
	}
}